var darId = getQueryString("id")
$(function () {
    $("#jqGrid").Grid({
        url: '../plandrafile/list?darId=' + darId,
        colModel: [
            {label: '文件名称', name: 'fileName', index: 'FILE_NAME', width: 80},
            {label: '资料类型', name: 'fileType', index: 'FILE_TYPE', width: 80},
            {label: '文件地址', name: 'url', index: 'URL', width: 80},
            {label: 'id', name: 'id', index: 'ID', key: true, hidden: true},
        ]
    });
});

var vm = new Vue({
    el: '#rrapp',
    data: {
        showList: true,
        title: null,
        planDraFile: {
            updateDate: '',
            updateUser: '',
            createDate: '',
            createUser: '',
            delFlag: '',
            fileId: '',
            fileName: '',
            fileType: '',
            url: '',
            draId: '',
            id: '',
        },
        ruleValidate: {
            name: [
                {required: true, message: '名称不能为空', trigger: 'blur'}
            ]
        },
        q: {
            name: ''
        },
        picUrl: ''
    },
    methods: {
        query: function () {
            vm.reload();
        },
        add: function () {
            vm.showList = false;
            vm.title = "新增";
            vm.planDraFile = {
                updateDate: '',
                updateUser: '',
                createDate: '',
                createUser: '',
                delFlag: 1,
                fileId: '',
                fileName: '',
                fileType: '',
                url: '',
                draId: darId,
            };
        },
        update: function (event) {
            var id = getSelectedRow("#jqGrid");
            if (id == null) {
                return;
            }
            vm.showList = false;
            vm.title = "修改";

            vm.getInfo(id);
        },
        saveOrUpdate: function (event) {
            var url = vm.planDraFile.id == null ? "../plandrafile/save" : "../plandrafile/update";
            Ajax.request({
                url: url,
                params: JSON.stringify(vm.planDraFile),
                type: "POST",
                contentType: "application/json",
                successCallback: function (r) {
                    alert('操作成功', function (index) {
                        vm.reload();
                    });
                }
            });
        },
        del: function (event) {
            var ids = getSelectedRows("#jqGrid");
            if (ids == null) {
                return;
            }

            confirm('确定要删除选中的记录？', function () {
                Ajax.request({
                    url: "../plandrafile/delete",
                    params: JSON.stringify(ids),
                    type: "POST",
                    contentType: "application/json",
                    successCallback: function () {
                        alert('操作成功', function (index) {
                            vm.reload();
                        });
                    }
                });
            });
        },
        getInfo: function (id) {
            Ajax.request({
                url: "../plandrafile/info/" + id,
                async: true,
                successCallback: function (r) {
                    vm.planDraFile = r.planDraFile;
                    var url = vm.planDraFile.url;
                    if (url) vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length);
                }
            });
        },
        reload: function (event) {
            vm.showList = true;
            var page = $("#jqGrid").jqGrid('getGridParam', 'page');
            $("#jqGrid").jqGrid('setGridParam', {
                postData: {'name': vm.q.name},
                page: page
            }).trigger("reloadGrid");
            vm.handleReset('formValidate');
        },
        reloadSearch: function () {
            vm.q = {
                name: ''
            };
            vm.reload();
        },
        handleSubmit: function (name) {
            handleSubmitValidate(this, name, function () {
                vm.saveOrUpdate()
            });
        },
        handleReset: function (name) {
            handleResetForm(this, name);
        },
        handleFormatError: function (file) {
            this.$Notice.warning({
                title: '文件格式不正确',
                desc: '文件 ' + file.name + ' 格式不正确，请上传格式正确的图片。'
            });
        },
        handleMaxSize: function (file) {
            this.$Notice.warning({
                title: '超出文件大小限制',
                desc: '文件 ' + file.name + ' 太大，不能超过 2M。'
            });
        },
        handleSuccess: function (res, file) {
            this.$Loading.finish();
            iview.Message.success('文件上传成功');
            vm.planDraFile.url = file.response.url;
            var url = vm.planDraFile.url;
            if (url) vm.picUrl = url.substring((url.lastIndexOf("\/") + 1), url.length);
        },
        eyeFile: function (url) {
            if (url) {
                window.open(url);
            } else {
                alert('还未上传文件');
            }
        }
    }
});